﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.IO;


public partial class TreeView : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetDrive();
            PopulateTree();
        }        
    }

    private void SetDrive()
    {
        ddlDrive.DataSource = Environment.GetLogicalDrives();
        ddlDrive.DataBind();
    }

    public void PopulateTree()
    {
        tvFolders.Nodes.Clear();

        string path = ddlDrive.Text;
        CreateChildNode(path);
    }

    private void CreateChildNode(string path)
    {
        CreateNode(path, null);
    }

    private void CreateNode(string path, TreeNode dNode)
    {
        if (Directory.Exists(path))
        {
            var rootFolders = Directory.EnumerateDirectories(path);
            foreach (string item in rootFolders)
            {
                TreeNode node = new TreeNode();
                node.Text = Path.GetFileName(item);
                node.Value = item;
                node.ImageUrl = "Images/folder_icon_small.gif";
                node.ChildNodes.Add(new TreeNode());
                node.Collapse();
                if (dNode == null)
                {
                    tvFolders.Nodes.Add(node);
                }
                else
                {
                    dNode.ChildNodes.Add(node);
                }
            }
            if (IsFolder(path))
            {
                var files = Directory.EnumerateFiles(path);
                foreach (string item in files)
                {
                    TreeNode node = new TreeNode();
                    node.ImageUrl = "Images/small_pdf.gif";
                    node.Text = Path.GetFileName(item);
                    node.Value = item;
                    if (dNode == null)
                    {
                        tvFolders.Nodes.Add(node);
                    }
                    else
                    {
                        dNode.ChildNodes.Add(node);
                    }
                }
            }
        }
    }
    
    protected void ddlDrive_SelectedIndexChanged(object sender, EventArgs e)
    {
        PopulateTree();
    }
    protected void tvFolders_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        TreeNode node = e.Node;
        node.ChildNodes.Clear();
        if (node.Value != null)
        {
            CreateNode(node.Value, node);
        }
    }

    private bool IsFolder(string path)
    {
        bool folder = false;
        FileAttributes attr = File.GetAttributes(path);

        //detect whether its a directory or file
        if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
        {
            folder = true;
        }
        return folder;
    }
    
}